package com.simtop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.simtop.dao.ResourcesMapper;
import com.simtop.pojo.resource.Resources;
import com.simtop.service.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lvyongwei
 */
@Service
public class ResourcesServiceImpl implements ResourcesService {

    @Autowired
    private ResourcesMapper resourcesMapper;

    /**
     * 添加资源
     * @param resources
     * @return
     */
    @Override
    public boolean insertResources(Resources resources) {
        return resourcesMapper.insertResources(resources);
    }

    /**
     * 修改资源
     * @param resources
     * @return
     */
    @Override
    public boolean updateResources(Resources resources) {
        return resourcesMapper.updateResources(resources);
    }

    /**
     * 删除资源
     * @param ids
     * @return
     */
    @Override
    public boolean deleteResources(Long[] ids) {
        return resourcesMapper.deleteResources(ids);
    }

    /**
     * 根据用户id获取资源内容
     * @param userId
     * @return
     */
    @Override
    public JSONObject selectResources(Integer userId) {
        List<Resources> userResources = resourcesMapper.selectResources(userId);
        userResources.forEach(re -> {
            String name = re.getName();
            re.setSuffix(name.substring(name.lastIndexOf("."),name.length()));
            re.setName(name.substring(0,name.lastIndexOf(".")));
        });
        List<Resources> photo = new ArrayList<Resources>();
        List<Resources> text = new ArrayList<Resources>();
        List<Resources> voice = new ArrayList<Resources>();
        List<Resources> video = new ArrayList<Resources>();
        for (Resources resource : userResources) {
            if(resource.getType().equals("photo")){
                photo.add(resource);
            }
            if(resource.getType().equals("text")){
                text.add(resource);
            }
            if(resource.getType().equals("voice")){
                voice.add(resource);
            }
            if(resource.getType().equals("video")){
                video.add(resource);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("photo",photo);
        jsonObject.put("text",text);
        jsonObject.put("voice",voice);
        jsonObject.put("video",video);
        return jsonObject;
    }

    /**
     * 获取自己的资源信息
     * @param userId
     * @return
     */
    @Override
    public List<Resources> getSelfResources(Integer userId) {
        List<Resources> resources = resourcesMapper.selectResources2(userId);
        resources.forEach(re -> {
            String name = re.getName();
            re.setSuffix(name.substring(name.lastIndexOf("."),name.length()));
            re.setName(name.substring(0,name.lastIndexOf(".")));
        });
        return resources;
    }

    /**
     * 根据资源id获取资源审核结果
     * @param ids
     * @return
     */
    @Override
    public List<Resources> getResources(Long[] ids) {
        List<Resources> resources = resourcesMapper.getResources(ids);
        resources.forEach(re -> {
            String name = re.getName();
            re.setSuffix(name.substring(name.lastIndexOf("."),name.length()));
            re.setName(name.substring(0,name.lastIndexOf(".")));
        });
        return resources;
    }
}
